package com.sunray;

import net.roseboy.classfinal.util.LibUtil;

import java.io.IOException;
import java.util.Arrays;

public class FinalPwd {
    public static String runtime_win_32 = "final32.dll";
    public static String runtime_win_64 = "final64.dll";
    public static String runtime_linux = "final.so";
    public static String runtime_linux_armhf = "finalarm.so";
    public static String runtime_linux_arm64 = "finalarm64.so";

    static {
        init();
//        System.load("D:\\project\\java\\socket-test\\src\\main\\java\\hello.dll");
//        System.load("D:\\project\\java\\socket-test\\src\\main\\java\\final64.dll");
    }

    public static void init() {
        String operatingSystem = System.getProperty("os.name");
        String architecture = System.getProperty("os.arch");

        try {
            if (operatingSystem.indexOf("Windows") != -1) {
                if (architecture.equals("x86_64") || architecture.equals("amd64")) {
                    LibUtil.loadLib(runtime_win_64);
                } else {
                    LibUtil.loadLib(runtime_win_32);
                }
            } else if (operatingSystem.indexOf("Linux") != -1) {
                if (architecture.equals("x86_64") || architecture.equals("amd64")) {
                    LibUtil.loadLib(runtime_linux);
                } else if (architecture.equals("arm")) {
                    LibUtil.loadLib(runtime_linux_armhf);
                } else if (architecture.equals("aarch64")) {
                    LibUtil.loadLib(runtime_linux_arm64);
                } else {
                    LibUtil.loadLib(runtime_linux);
                }
            } else {
                LibUtil.loadLib(runtime_linux);
            }
        } catch (UnsatisfiedLinkError | IOException e) {
            if (e.getMessage().indexOf("already loaded in another classloader")!= -1) {
                System.err.println("already loaded in another classloader");
                e.printStackTrace();
//                throw e;
            } else {
                System.err.println("load so failed:" + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        char[] cs = FinalPwd.getChars();
        for (char c : cs) {
            System.out.println((byte) c);
        }
        System.out.println(Arrays.toString(new FinalPwd().getChars()));
    }

    public static native char[] getChars();
}
